"""
__author__ = 'berlinyoung'
@File   :app.py
@Date   :2021/10/7 10:10
-*- coding: utf-8 -*-
"""
import os
import sys

from appium import webdriver

from test_app.test_po5.page.base_page import BasePage


class App(BasePage):
    def start(self):
        udid = os.getenv('udid')
        port = os.getenv('port')
        # 通过sys.argv[]的方式来获取udid的话。
        # 命令python -m pytest test_contact.py --udid='127.0.0.1:7555' --port=4725
        # pytest是第0个参数，test_contact.py是第1个参数，udid是第2个参数，port是第3个参数
        # udid = sys.argv[2].split('=')[1]
        # port = sys.argv[3].split('=')[1]
        if self.driver == None:
            print("driver is None")
            # 创建json串
            caps = {}
            caps["platformName"] = "android"
            caps["deviceName"] = "127.0.0.1:7555"
            caps["appPackage"] = "com.tencent.wework"
            caps["appActivity"] = ".launch.LaunchSplashActivity"
            caps["noReset"] = "true"
            caps["ensureWebviewsHavePages"] = True
            caps["udid"] = udid
            # 最重要的一行，完成了客户端与服务端的连接，创建driver
            self.driver = webdriver.Remote(f"http://127.0.0.1:{port}/wd/hub", caps)
            # self.driver = webdriver.Remote("http://127.0.0.1:4723/wd/hub", caps)
            # 隐式等待，每次在调用find_element/ find_elements方法的时候激活。在3s内动态的去执行下一步
            self.driver.implicitly_wait(3)
        else:
            print("driver is not None")
            # launch_app() 启动页面 热启动
            self.driver.launch_app()
        return self

    def restart(self):
        pass

    def stop(self):
        # 销毁 driver
        self.driver.quit()

    def goto_main(self):
        #入口
        from test_app.test_po5.page.main_page import MainPage
        return MainPage(self.driver)